import Character from '../../../../miao-plugin/models/Character.js'
import Meta from '../../../../miao-plugin/components/Meta.js'

/**
 * 在给定的文本中搜索与别名对象中任何别名匹配的字符串
 * @param {string} text 要搜索的文本
 * @param {boolean} isSr 是否为星铁
 * @returns {Promise<string|null>} 如果找到匹配的别名，则返回该别名；否则返回 null
 */

async function findCharacter (text, isSr) {
  const game = isSr ? 'sr' : 'gs'
  for (const nickname of Meta.getAlias(game, 'char')) {
    if (text.includes(nickname)) {
      return nickname
    }
  }
  return null
}

export default class Button {
    constructor() {
      this.plugin = {
        name: "角色养成",
        dsc: "角色养成",
        priority: 100,
        rule: [
          {
            reg: '^#*(星铁)?(.*)(养成|计算)([0-9]|,|，| )*$',
            fnc: 'buttonCenter'
          },
          {
            reg: '^#*(星铁)?角色(养成|计算|养成计算)$',
            fnc: 'buttonCenter'
          }
        ]
      }
    }
    async buttonCenter(e){
      const game = (e.game === 'sr' || e.isSr) ? '星铁' : ''
      const name = await findCharacter(e.raw_message, game)
    if (!name) return false
    let material = ''
    if (!game) {
      material = Character.get(name).getMaterials()
        .find(material => material.num == 168)
    }
      const list = [
        [
        { label: `${name}养成`, data: `/${game}${name}养成` },
        ],[
        { label: '更新面板', callback: `/${game}更新面板` },
        ]
      ]
      return Bot.Button(list)
    }
  }